package com.huonan.jb.tools.esuse.export.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.setting.dialect.Props;
import com.huonan.jb.tools.esuse.config.SplitTime;
import com.huonan.jb.tools.esuse.dto.EsHitsInfo;
import com.huonan.jb.tools.esuse.export.enums.IndexType;
import com.huonan.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class EsUtils {

    public static final String INDEXFORMAT = "ik_sl_v2_%s_ec";

    public static Props getProps(int threadSize, int index, String argFile) {
        Props props = new Props(argFile);
        String oldSql = props.getStr("sql");
        String timeField = props.getStr("timeField");
        if (StringUtils.isNotEmpty(timeField)) {
            String startTime = props.getStr("timeRange").split(",")[0];
            String endTime = props.getStr("timeRange").split(",")[1];
            SplitTime tT = new SplitTime(threadSize, index, startTime, endTime).invoke();
            String newStartTime = tT.getNewStartTime();
            String newEndTime = tT.getNewEndTime();
            String newSql = String.format(oldSql, timeField, newStartTime, newEndTime);
            System.out.println(newSql);
            props.setProperty("sql", newSql);
            props.setProperty("startTime", newStartTime);
            props.setProperty("endTime", newEndTime);
        }
        return props;
    }

     public static String getFilePath(Props props, String subFile) {
        String baseDir = System.getProperty("user.dir");
        String filePath = baseDir + props.getStr("outFile") + FileUtil.FILE_SEPARATOR + DateUtil.today();
        String count = props.getStr("count");
        String name = Thread.currentThread().getName();
        System.out.println(name);
        String fileName;
        if (StrUtil.isEmpty(props.getStr("startTime"))) {
            fileName = DateUtil.today();
        } else {
            fileName = props.getStr("startTime") + "-" + props.getStr("endTime");
        }
        String format = String.format("%s/%s-%s.%s", filePath, fileName, count, subFile);
        log.info("输出文件: {}", format);
        return format;
    }

    public static boolean isContinue(EsHitsInfo dataResult) {
        return BeanUtil.isEmpty(dataResult);
    }

    public static boolean isBreak(JSONArray hits) {
        if (hits == null || hits.size() == 0) {
            log.info(Thread.currentThread().getName() + " 【查询结束】");
            return true;
        }
        return false;
    }

    public static List<String> getAllIndex(IndexType indexType) {
        // 获取当前日期
        LocalDate now = LocalDate.now();
        List<String> days = getDays();
        if (indexType.equals(IndexType.MONTH)) {
            return days.stream().map(m -> m.substring(0, 6))
                    .map(tmpDay -> String.format(INDEXFORMAT, tmpDay))
                    .distinct().collect(Collectors.toList());
        } else {
            return days.stream().map(tmpDay -> String.format(INDEXFORMAT, tmpDay)).collect(Collectors.toList());
        }

    }


    public static List<String> getDays() {
        LinkedList<String> daysInYear1 = DateUtils.getDaysInYear(2021);
        LinkedList<String> daysInYear2 = DateUtils.getDaysInYear(2022);
        LinkedList<String> daysInYear3 = DateUtils.getDaysInYear(2023);
        List<String> list = new ArrayList<>();
        list.addAll(daysInYear1);
        list.addAll(daysInYear2);
        list.addAll(daysInYear3);
//        List<String> test = Collections.singletonList("ik_sl_v2_20230514_video");
//        return test;
        // 获取当前日期
        LocalDate now = LocalDate.now();
        LocalDate thirtyMonthsAgo = now.minus(30, ChronoUnit.MONTHS);
        int formatBefore30 = Integer.parseInt(DateUtil.format(thirtyMonthsAgo.atStartOfDay(), "yyyyMMdd"));
        int formatToday = Integer.parseInt(DateUtil.format(now.atStartOfDay(), "yyyyMMdd"));
        // 获取30个月前的时间
        return list.stream().filter(d -> {
            int parseNowDate = Integer.parseInt(d);
            return parseNowDate >= formatBefore30 && parseNowDate <= formatToday;
        }).collect(Collectors.toList());
    }

    public static void main(String[] args) {
        List<String> allIndex = getAllIndex(IndexType.DAY);
        System.out.println(allIndex);
        System.out.println(allIndex.size());
    }
}
